package com.balabala.utils;

import com.balabala.pojo.JdResult;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: SuMaLi
 * \* Date: 2020/7/9
 * \* Time: 14:43
 * \
 */
public class HtmlParseUtils {

    public static List<JdResult> parseHtml(String keyWord) throws IOException {
        //获得请求  https://search.jd.com/Search?keyword=java
        String url = "https://search.jd.com/Search?keyword=" + keyWord;
        //解析网页
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");
        List<JdResult> jdResults = new ArrayList<>();
        for (Element element1 : elements) {
            JdResult jdResult = new JdResult();
            String imgUrl = element1.getElementsByTag("img").eq(0).attr("src");
            jdResult.setImgUrl(imgUrl);
            String price = element1.getElementsByClass("p-price").eq(0).text();
            jdResult.setPrice(price);
            String title = element1.getElementsByClass("p-name").eq(0).text();
            jdResult.setTitle(title);
            jdResults.add(jdResult);
        }
        return jdResults;
    }

}
